<template>
  <div class="dashboard-container">
    <div class="app-container">
      <template>
        <div class="dashboard-container">
          <div class="app-container">
            <el-card>
              <el-tabs>
                <!-- 放置页签 -->
                <el-tab-pane label="角色管理">
                  <!-- 新增角色按钮 -->
                  <el-row style="height:60px">
                    <el-button
                      icon="el-icon-plus"
                      size="small"
                      type="primary"
                      @click="showDialog=true"
                    >新增角色</el-button>
                  </el-row>
                  <!-- 表格 -->
                  <el-table border :data="list">
                    <el-table-column label="序号" type="index" width="120" />
                    <el-table-column label="角色名称" prop="name" width="240" />
                    <el-table-column label="描述" prop="description" />
                    <el-table-column label="操作">
                      <template slot-scope="{row}">
                        <el-button size="small" type="success">分配权限</el-button>
                        <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                        <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                      </template>

                    </el-table-column>
                  </el-table>
                  <!-- 分页组件 -->
                  <el-row type="flex" justify="center" align="middle" style="height: 60px">
                    <!-- 分页组件 -->
                    <el-pagination :total="q.total" :current-page="q.page" :page-size="q.pagesize" layout="total,prev,pager,next" @current-change="changePage" />
                  </el-row>
                </el-tab-pane>
                <el-tab-pane label="公司信息">
                  <el-alert
                    title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
                    type="info"
                    show-icon
                    :closable="false"
                  />
                  <el-form :model="formData" label-width="120px" style="margin-top:50px">
                    <el-form-item label="公司名称">
                      <el-input v-model="formData.name" disabled style="width:400px" />
                    </el-form-item>
                    <el-form-item label="公司地址">
                      <el-input v-model="formData.companyAddress" disabled style="width:400px" />
                    </el-form-item>
                    <el-form-item label="邮箱">
                      <el-input v-model="formData.mailbox" disabled style="width:400px" />
                    </el-form-item>
                    <el-form-item label="备注">
                      <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
                    </el-form-item>
                  </el-form>
                </el-tab-pane>

              </el-tabs>
            </el-card>
          </div>
          <!-- 弹出层 -->
          <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
            <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleForm.name" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input v-model="roleForm.description" />
              </el-form-item>
            </el-form>
            <!-- 底部 -->
            <el-row slot="footer" type="flex" justify="center">
              <el-col :span="6">
                <el-button size="small" @click="btnCancel">取消</el-button>
                <el-button size="small" type="primary" @click="btnOK">确定</el-button>
              </el-col>
            </el-row>
          </el-dialog>
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, delRoleList, getRoleDetail, editRoleList, addRoleList } from '@/api/setting'
export default {
  data() {
    return {
      q: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      list: [],
      formData: {},
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述信息不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    companyId() {
      return this.$store.getters.companyId
    },
    showTitle() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取列表
    async getRoleList() {
      const { rows, total } = await getRoleList(this.q)
      this.list = rows
      this.q.total = total
    },
    // 翻页
    changePage(newpage) {
      this.q.page = newpage
      this.getRoleList()
    },
    // 获取公司详情
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 删除角色
    async  deleteRole(id) {
      try {
        await this.$confirm('确认删除吗')
        await delRoleList(id)
        debugger
        if (this.q.page > 1 && this.list.length === 1) {
          this.q.page--
          this.getRoleList()
        }
        this.getRoleList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑角色
    async editRole(id) {
      this.showDialog = true
      this.roleForm = await getRoleDetail(id)
    },
    btnCancel() {
      // 重置数据
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()
        let info = ''
        if (this.roleForm.id) {
          info = '编辑'
          await editRoleList(this.roleForm)
        } else {
          info = '新增'
          await addRoleList(this.roleForm)
        }

        this.getRoleList()
        this.showDialog = false
        this.$message.success(`${info}成功`)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
